package com.minxi.web.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.PreparedStatement;

@WebServlet(name = "JspUpdateServlet",urlPatterns = "/jspUpdate")
public class JspUpdateServlet  extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //1.获取修改按钮传过来的 id值
        String id = req.getParameter("id");
        //2.通过使用id值去数据库查询数据
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //2.请求数据库 JDBC
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            //3.编辑SQL语句 (预编译的执行对象)
            String sql = "select id,user_name,user_age,user_sex,user_phone from user_info where id = ?;";
            //得到执行对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,Integer.parseInt(id));
            ResultSet resultSet = preparedStatement.executeQuery();
            //3.把数据封装成 UserInfo对象
            UserInfo userInfo = new UserInfo();
            while (resultSet.next()){
                int userId = resultSet.getInt("id");
                String userName = resultSet.getString("user_name");
                int userAge = resultSet.getInt("user_age");
                int userSex = resultSet.getInt("user_sex");
                String userPhone = resultSet.getString("user_phone");
                //怎么来封装这些字段数据？ 使用字符串比较麻烦，不好在页面展示
                userInfo.setId(Long.parseLong(String.valueOf(userId)));
                userInfo.setUserName(userName);
                userInfo.setUserAge(userAge);
                userInfo.setUserSex(userSex);
                userInfo.setUserPhone(userPhone);
            }
            //4.把封装好的数据放到session会话域中，以便于页面上可以获取到
            //如何将list放到页面上使用 HttpServletRequest Session(会话域)
            HttpSession session1 = req.getSession();
            //K V结构
            session1.setAttribute("userInfo",userInfo);
            //跳转到指定的页面
            resp.sendRedirect("jsp/update.jsp");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(null != preparedStatement){
                    preparedStatement.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
